/**
 * Created by oneminute on 7/24/14.
 */

var lz = require("../utils/modules")();
var moment = require("moment");

function Message(_opts) {
    lz.Persistent.call(this, _opts);
    if (!_opts) _opts = {
        header: {},
        body: {}
    };
    _opts.header = _opts.header || {};
    _opts.body = _opts.body || {};
    var now = moment();
    this.__properties.pushArray([
        {
            name: "header",
            value: {
                mid: _opts.header.mid || null,
                timestamp: _opts.header.timestamp || now.format(lz.Config.datetimeFormat),
                senderid: _opts.header.senderid || null,
                recieverid: _opts.header.recieverid || null
            }
        },
        {
            name: "body",
            value: _opts.body || null
        }
    ]);
    this.__initProperties();
    this.__collection = lz.Config.db.arango.messages;
    this.__aql = lz.Config.db.arango.aqls.GET_OBJECT_BY_PROPERTY;
}

require("util").inherits(Message, lz.Persistent);

Message.prototype.loadOfflineMessages = function (_callback) {
    var self = this;
    if (!self.header.recieverid) {
        _callback(null, null);
    }

    self.list({
        method: "byAQL",
        aql: lz.Config.db.arango.aqls.GET_OFFLINE_MESSAGES,
        params: {
            "@collection": self.__collection,
            "value": self.header.recieverid
        }}, _callback);
};

exports = module.exports = Message;
